package com.ruoyi.psi.purchase.order.service;


import com.ruoyi.DingDing.Domain.DingDingText;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.oa.approve.domain.ApproveDomain;
import com.ruoyi.personnel.oa.approve.mapper.ApproveMapper;
import com.ruoyi.personnel.oa.sequence.service.IApproveSequenceService;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApplyList;
import com.ruoyi.psi.purchase.apply.mapper.PurchaseApplyMapper;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrderList;
import com.ruoyi.psi.purchase.order.mapper.PurchaseOrderMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购订单
 * 业务执行层
 *
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Autowired
    private PurchaseApplyMapper purchaseApplyMapper;
    @Autowired
    private IdUtil idUtil;
    @Autowired
    private ApproveMapper approveMapper;
    @Autowired
    private IApproveSequenceService approveSequenceService;

    /**
     * 采购订单查询
     *
     * @param purchaseOrder
     * @return
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder) {
        List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
        for (int i = 0; i < purchaseOrders.size(); i++) {
            if (StringUtils.isNotNull(purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrders.get(i).getPurchaseOrderId()))) {
                //存在，则存入采购订单对应产品信息
                purchaseOrders.get(i).setPurchaseOrderLists(purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrders.get(i).getPurchaseOrderId()));
            }
        }
        return purchaseOrders;
    }

    /**
     * 按编号查询
     *
     * @param purchaseOrderId 采购订单编号
     * @return
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(String purchaseOrderId) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectPurchaseOrderById(purchaseOrderId);
        if (StringUtils.isNotNull(purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrder.getPurchaseOrderId()))) {
            //存在，则存入采购订单对应产品信息
            purchaseOrder.setPurchaseOrderLists(purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrder.getPurchaseOrderId()));
        }
        return purchaseOrder;
    }

    /**
     * 采购订单新增
     *
     * @param purchaseOrder
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurchaseOrder(PurchaseOrder purchaseOrder) throws Exception {
        String purchaseOrderId = idUtil.createPurchaseOrderId();//自动生成编号
        if (StringUtils.isNotNull(purchaseOrderMapper.selectPurchaseOrderById(purchaseOrderId))) {
            //存在
            return 0;
        }
        //不存在
        purchaseOrder.setPurchaseOrderId(purchaseOrderId);// 主表添加编号
        ApproveDomain approve = new ApproveDomain(purchaseOrderId, "采购申请", ERPConstant.Approval_Type_Purchase, purchaseOrder.getStaffId()); //新增审批
        approve.setCurrentApprover(approveSequenceService.getLowestRoleId(ERPConstant.Approval_Type_Purchase));
        approveMapper.insertApprove(approve);
        Long currentApprover = approve.getCurrentApprover();
        DingDingText dingDingText = new DingDingText();
        dingDingText.BatchRobotText(currentApprover);
        purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);//采购订单据
        List<PurchaseOrderList> purchaseOrderLists = purchaseOrder.getPurchaseOrderLists();//采购订单产品信息
        for (int i = 0; i < purchaseOrderLists.size(); i++) {
            purchaseOrderLists.get(i).setPurchaseOrderId(purchaseOrderId); // 子表添加编号
            purchaseOrderMapper.insertPurchaseOrderList(purchaseOrderLists.get(i));//采购订单产品信息
        }
        return 1;
    }

    /**
     * 根据申请单详情采购订单新增
     * 采购跟单
     *
     * @param purchaseOrder
     * @return
     * @throws Exception
     */
    @Override
    public int insertPurchaseOrderByPurchaseApplyList(PurchaseOrder purchaseOrder) throws Exception {
//        //存入采购订单编号
//        purchaseOrder.setPurchaseOrderId(idUtil.createPurchaseOrderId());
//        purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);//采购订单单据
//        List<PurchaseOrderList> purchaseOrderLists = purchaseOrder.getPurchaseOrderLists();//采购订单产品信息
//        for (int i = 0; i < purchaseOrderLists.size(); i++) {
//            purchaseOrderLists.get(i).setPurchaseOrderId(purchaseOrder.getPurchaseOrderId());
//
//            // 将采购申请单产品的采购数量复制到采购订单产品数量 修改by641
//            PurchaseApplyList purchaseApplyList = new PurchaseApplyList(purchaseOrder.getPurchaseOrderLists().get(i));
//            purchaseOrderLists.get(i).setPurchaseOrderNumber(
//                    purchaseApplyMapper.selectPurchaseApplyListById(purchaseApplyList.getPurchaseApplyId(), purchaseApplyList.getProduceId()).getPurchaseApplyNumber()
//            );
//
//            purchaseOrderMapper.insertPurchaseOrderList(purchaseOrderLists.get(i));//采购订单产品信息
//        }
//        for (int i = 0; i < purchaseOrder.getPurchaseOrderLists().size(); i++) {
//            //更新采购订单采购详情的申请状态
//            purchaseApplyMapper.updatePurchaseApplyListStatus(new PurchaseApplyList(purchaseOrder.getPurchaseOrderLists().get(i)));
//        }
        if (StringUtils.isNotNull(purchaseOrderMapper.selectPurchaseOrderById(purchaseOrder.getPurchaseOrderId()))) {
            //存在
            return 0;
        }
        purchaseOrderService.insertPurchaseOrder(purchaseOrder);
        for (int i = 0; i < purchaseOrder.getPurchaseOrderLists().size(); i++) {
            //更新采购订单采购详情的申请状态
            purchaseApplyMapper.updatePurchaseApplyListStatus(new PurchaseApplyList(purchaseOrder.getPurchaseOrderLists().get(i)));
        }
        return 1;
    }

    /**
     * 采购订单更新
     *
     * @param purchaseOrder
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder) throws Exception {
        if (!StringUtils.isNotNull(purchaseOrderMapper.selectPurchaseOrderById(purchaseOrder.getPurchaseOrderId()))) {
            //不存在
            return 0;
        }
        //存在采购订单单据
        purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);//采购订单单据
        List<PurchaseOrderList> purchaseOrderLists1 = purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrder.getPurchaseOrderId());
        List<PurchaseOrderList> purchaseOrderLists = purchaseOrder.getPurchaseOrderLists();//采购订单产品信息
        for (int i = 0; i < purchaseOrderLists.size(); i++) {
            PurchaseOrderList purchaseOrderList = purchaseOrderMapper.selectPurchaseOrderListById(purchaseOrder.getPurchaseOrderId(), purchaseOrderLists.get(i).getProduceId());
            if (StringUtils.isNotNull(purchaseOrderList)) {
                //存在采购订单详细
                for (int j = 0; j < purchaseOrderLists1.size(); j++) {
                    if (purchaseOrderLists1.get(j).getProduceId().equals(purchaseOrderList.getProduceId())) {
                        purchaseOrderLists1.remove(j);//移除新旧都有的部分
                        break;
                    }
                }//更新
                purchaseOrderMapper.updatePurchaseOrderList(purchaseOrderLists.get(i));
            } else {//新增
                purchaseOrderMapper.insertPurchaseOrderList(purchaseOrderLists.get(i));
            }
        }
        for (int i = 0; i < purchaseOrderLists1.size(); i++) {
            //删除新旧重叠以外的部分
            purchaseOrderMapper.deletePurchaseOrderListById(purchaseOrderLists1.get(i).getPurchaseOrderId(), purchaseOrderLists1.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新采购订单删除标志位
     *
     * @param purchaseOrder
     * @return
     */
    @Override
    public int updatePurchaseOrderDeleteFlag(PurchaseOrder purchaseOrder) {
        return purchaseOrderMapper.updatePurchaseOrderDeleteFlag(purchaseOrder);
    }

    /**
     * 采购订单详细查询
     *
     * @param purchaseOrderList
     * @return
     */
    @Override
    public List<PurchaseOrderList> selectPurchaseOrderListList(PurchaseOrderList purchaseOrderList) {
        return purchaseOrderMapper.selectPurchaseOrderListList(purchaseOrderList);
    }

    /**
     * 查询采购订单对应的所有产品详细
     *
     * @param purchaseOrderId 采购订单编号
     * @return
     */
    @Override
    public List<PurchaseOrderList> selectPurchaseOrderListListById(String purchaseOrderId) {
        return purchaseOrderMapper.selectPurchaseOrderListListById(purchaseOrderId);
    }

    /**
     * 查询采购订单对应的指定产品详细
     *
     * @param purchaseOrderId
     * @param produceId
     * @return
     */
    @Override
    public PurchaseOrderList selectPurchaseOrderListById(String purchaseOrderId, String produceId) {
        return purchaseOrderMapper.selectPurchaseOrderListById(purchaseOrderId, produceId);
    }

    /**
     * 查询所有未入库的采购订单
     * @param purchaseOrder
     * @return
     */
    @Override
    public List<PurchaseOrder> selectAllNotStoragePurchaseOrderList(PurchaseOrder purchaseOrder) {
        return purchaseOrderMapper.selectAllNotStoragePurchaseOrderList(purchaseOrder);
    }

    /**
     * 查询某未入库采购订单的详情信息
     * @param purchaseOrderId
     * @return
     */
    @Override
    public List<PurchaseOrderList> selectNotStoragePurchaseOrderById(String purchaseOrderId) {
        return purchaseOrderMapper.selectNotStoragePurchaseOrderById(purchaseOrderId);
    }

    /**
     * 查询某未质检的采购订单的详情信息
     * @param purchaseOrderId
     * @return
     */
    @Override
    public List<PurchaseOrderList> selectNotInspectPurchaseOrderById(String purchaseOrderId) {
        return purchaseOrderMapper.selectNotInspectPurchaseOrderById(purchaseOrderId);
    }
}
